package com.yongcheon.sms_manager.ui.sms_spam.list;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.data.sms_spam.SMSData;
import com.yongcheon.sms_manager.data.sms_spam.SpamData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.ContactVO;
import com.yongcheon.sms_manager.vo.SMSVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-27.
 */
public class SPAMLongClickListener implements View.OnLongClickListener{

    SMSVO smsvo;
    ContactVO contactvo;
    SPAMCurosrAdapter cursorAdapter;

    public SPAMLongClickListener(SMSVO smsvo, ContactVO contactvo,SPAMCurosrAdapter cursorAdapter){
        super();
        this.smsvo = smsvo;
        this.contactvo = contactvo;
        this.cursorAdapter = cursorAdapter;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] MENU_LIST = {v.getContext().getResources().getString(R.string.delete_this_sms), v.getContext().getResources().getString(R.string.delete_same_number),
                              v.getContext().getResources().getString(R.string.unregister_spam)};
        String from = (contactvo != null) ? contactvo.getName() : smsvo.getAddress();
        AlertDialog dialog = new AlertDialog.Builder(v.getContext())
                .setTitle(from)
                .setItems(MENU_LIST, new DialogOnClickListener(v.getContext()))
                .create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return false;
    }

    class DialogOnClickListener implements  DialogInterface.OnClickListener{
        private Context context;

        public DialogOnClickListener(Context context){
            super();
            this.context = context;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            SMSData smsData = new SMSData();
            SpamData spamData = new SpamData(context);
            SpamNumberData spamNumData = new SpamNumberData(context);

            SQLiteDatabase database = SpamDBManager.getDatabase(context);
            switch (which){
                case 0 :
                    spamData.delete(SPAMLongClickListener.this.smsvo.get_Id(), context, database);
                    cursorAdapter.getCursor().requery();
                    break;
                case 1 :
                    spamData.deleteByPhoneNum(SPAMLongClickListener.this.smsvo.getAddress(), context, database);
                    cursorAdapter.getCursor().requery();
                break;
                case 2 :
                    if(DefaultAppUtil.isWritable(context)) unRegistSpam(database, spamData, smsData, spamNumData);
                    break;
            }
        }

        public void unRegistSpam(SQLiteDatabase database, SpamData spamData, SMSData smsData, SpamNumberData spamNumData){
            try {
                database.beginTransaction();

                ArrayList<SMSVO> smsList = spamData.select(SPAMLongClickListener.this.smsvo.getAddress(), context, database);

                for (int i = 0; i < smsList.size(); i++) {
                    spamData.delete(smsList.get(i).get_Id(), context, database);
                }

                for (int i = 0; i < smsList.size(); i++) {
                    smsData.insert(smsList.get(i), context, null);
                }

                spamNumData.delete(SPAMLongClickListener.this.smsvo.getAddress(), context, database);

                cursorAdapter.getCursor().requery();
                database.setTransactionSuccessful();
            }catch(Exception e){
                database.endTransaction();
                e.printStackTrace();
            }finally {
                database.endTransaction();
            }
        }
    }
}
